
import 'dart:core';

import 'package:dark_notes/utilities/storage.dart';
import 'package:flutter/material.dart';

class SettingsModel extends ChangeNotifier {

  Color _lightPrimaryColor = ThemeData.light().primaryColor;
  Color _lightAccentColor = ThemeData.light().accentColor;
  Color _darkPrimaryColor = ThemeData.dark().primaryColor;
  Color _darkAccentColor = ThemeData.dark().accentColor;

  bool _isDarkTheme = true;

  SettingsStorage storage;

  SettingsModel({@required this.storage}) {
    this.storage.readSettings().then((Settings settings) {
      settings.lightPrimaryColor != null ? _lightPrimaryColor = settings.lightPrimaryColor : _lightPrimaryColor = ThemeData.light().primaryColor;
      settings.lightAccentColor != null ? _lightAccentColor = settings.lightAccentColor : _lightAccentColor = ThemeData.light().accentColor;
      settings.darkPrimaryColor != null ? _darkPrimaryColor = settings.darkPrimaryColor : _darkPrimaryColor = ThemeData.dark().primaryColor;
      settings.darkAccentColor != null ? _darkAccentColor = settings.darkAccentColor : _darkAccentColor = ThemeData.dark().accentColor;
      settings.isDarkTheme != null ? _isDarkTheme = settings.isDarkTheme : _isDarkTheme = true;
    });
  }

  Color get lightPrimaryColor => _lightPrimaryColor;
  Color get lightAccentColor => _lightAccentColor;
  Color get darkPrimaryColor => _darkPrimaryColor;
  Color get darkAccentColor => _darkAccentColor;

  bool get isDarkTheme => _isDarkTheme;

  void changePrimary(Color color) {
    if(_isDarkTheme){
      _darkPrimaryColor = color;
    } else {
      _lightPrimaryColor = color;
    }
    persistSettings();
    notifyListeners();
  }

  void changeAccent(Color color) {
    if(_isDarkTheme) {
      _darkAccentColor = color;
    } else {
      _lightAccentColor = color;
    }
    persistSettings();
    notifyListeners();
  }

  void changeTheme(bool value) {
    _isDarkTheme = value;
    persistSettings();
    notifyListeners();
  }

  void persistSettings() {
    Settings settings = Settings(lightPrimaryColor: _lightPrimaryColor, lightAccentColor: _lightAccentColor, darkPrimaryColor: _darkPrimaryColor, darkAccentColor: _darkAccentColor, isDarkTheme: _isDarkTheme);
    this.storage.writeSettings(settings);
  }


}

class Settings {
  Color lightPrimaryColor;
  Color lightAccentColor;
  Color darkPrimaryColor;
  Color darkAccentColor;

  bool isDarkTheme;

  Settings({this.lightPrimaryColor, this.lightAccentColor, this.darkPrimaryColor, this.darkAccentColor, this.isDarkTheme});

  Settings.fromJson(Map<String, dynamic> data)
      : lightPrimaryColor = Color(data['light_primary_color']),
        lightAccentColor = Color(data['light_accent_color']),
        darkPrimaryColor = Color(data['dark_primary_color']),
        darkAccentColor = Color(data['dark_accent_color']),
        isDarkTheme = data['is_dark_theme'];

  Map<String, dynamic> toJson() =>
      {
        'light_primary_color': lightPrimaryColor.value,
        'light_accent_color': lightAccentColor.value,
        'dark_primary_color': darkPrimaryColor.value,
        'dark_accent_color': darkAccentColor.value,
        'is_dark_theme': isDarkTheme
      };
}